package sub

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func RemoveTime(subPath string, fPath string) error {

	arr, err := ReadFileLineByLine(subPath)
	if err != nil {
		return err
	}
	lines := RemoveTimeNow(arr)

	err = WriteToFile(lines, fPath)

	return err
}

func WriteToFile(lines []string, fPath string) error {

	file, err := os.Create(fPath)
	if err != nil {
		return err
	}
	defer file.Close()

	w := bufio.NewWriter(file)
	for _, line := range lines {
		fmt.Fprintln(w, line)
	}
	return w.Flush()

}

func RemoveTimeNow(arr []string) []string {

	var lines []string
	for _, line := range arr {

		if len(line) < 2 {

			lines = append(lines, line)
			continue
		}

		if strings.HasPrefix(line, "00:") {
			continue
		}
		if strings.HasPrefix(line, "01:") {
			continue
		}
		if strings.HasPrefix(line, "02:") {
			continue
		}
		lines = append(lines, line)
	}

	return lines
}

func ReadFileLineByLine(subPath string) ([]string, error) {

	file, err := os.Open(subPath)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	var lines []string
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}
	return lines, scanner.Err()

	// var fileLines []string
	// readFile, err := os.Open(subPath)
	// if err != nil {

	// 	return fileLines, err
	// }
	// fileScanner := bufio.NewScanner(readFile)
	// fileScanner.Split(bufio.ScanLines)

	// for fileScanner.Scan() {
	// 	fileLines = append(fileLines, fileScanner.Text())
	// }

	// readFile.Close()

	// // for _, line := range fileLines {
	// //     fmt.Println(line)
	// // }

	// return fileLines, nil

}
